<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleSave">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
		<!--列表-->
    <el-table :data="dictionarydetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
     
      <el-table-column prop="name" label="字典名称" width="120" sortable>
      </el-table-column>
      <el-table-column prop="types.name" label="字典类型" width="100" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small"  @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small"  @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

		<!--工具条 批量删除和分页栏-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="120px" :rules="saveFormRules" ref="saveForm">
       
				<el-form-item label="字典明细名称" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>

        <el-form-item label="字典明细类型" prop="typesId">
          <el-select v-model="saveForm.typesId" placeholder="请选择">
            <el-option v-for="item in types"
                       :label="item.name"
                       :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, saveUser } from '../../api/api';

	export default {
		data() {
			return {
				dictionarydetails: [],
				totals: 0,
        currentPage:1,      //当前页
        pageSize:5,         //每页显示的条数
				listLoading: false,
				sels: [],//列表选中列
        keyword:'',//高级查询
        title:'',//标题
        types:[],//所有类型

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入字典名称', trigger: 'blur' }
					]
				},

				saveFormVisible: false,//新增界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入字典名称', trigger: 'blur' }
					],
				},
				//新增界面数据
				saveForm: {
          id: null,
					name: '',
          typesId: null,
				}
			}
		},
		methods: {
			//1.获取用户列表
			getDictionarydetails() {
				let para = {
          pageSize: this.pageSize,
          currentPage: this.currentPage,
          keyword:this.keyword
				};
				this.listLoading = true;
        this.$http.post("/dictionarydetail",para).then(res =>{
          console.log(res)
          this.listLoading=false;
          this.dictionarydetails =  res.data.data
          this.totals =res.data.totals
          
        }).catch(res =>{
          this.$message.error("系统繁忙,请稍后再试...")
        })
			},
      //2.高级查询- 关键字查询
      keywordQuery(){
        this.currentPage=1;//高级查询  页数需要设置到第一页,如果页数在其他页数 当查询条数没有那么多, 查询出来的数据就是空的
        this.getDictionarydetails();
      },
      //3.点击分页
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getDictionarydetails();
      },
			//4.删除业务
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true //显示加载框
          this.$http.delete("/dictionarydetail/"+row.id).then(res=>{
            this.listLoading = false;//关闭加载框
            if(res.data.success){
              this.$message.success("删除成功") //提示删除成功
              //删除成功之后对当前页的处理:特殊情况:删除最后一页的最后一条数据
              var totalPage=Math.ceil(this.totals/this.pageSize);//总页数=最后一页
              //如果是当前页是最后一页             //并且是最后一页的最后一条数据            //并不是第一页
              if(this.currentPage==totalPage && (this.totals-1)%this.pageSize==0 && this.currentPage!=1){
                this.currentPage--;
              }
              this.getDictionarydetails();//删除后 重新查询数据
            }else {
              this.$message.error("系统繁忙,请稍后重试!!![500]")
            }
          }).catch(res=>{
            this.$message.error("系统繁忙,请稍后重试!!![400,404]")
          })
				}).catch(() => {

				});
			},
			//5.显示编辑界面
			handleEdit: function (index, row) {
        this.title="编辑";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
        this.getTypes()//打开新增框 加载部门经理下拉框列表列表

			},
			//6.显示新增界面
			handleSave: function () {
        this.title="新增";
				this.saveFormVisible = true;
				this.saveForm = {
          id: null,
          name: '',
          typesId: null,

				};
        this.getTypes()//打开新增框 加载部门经理下拉框列表列表
			},
			//7.新增 或者修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
              //如果选择多级部门， 需要判断，拿到数组的最后一个数字
              if (this.saveForm.parent_id){//如果不为空
                this.saveForm.parent_id = this.saveForm.parent_id[this.saveForm.parent_id.length-1]
              }

              this.$http.put("/dictionarydetail",this.saveForm).then(res =>{
                if (res.data.success){
                  	this.getDictionarydetails();
                  this.$message({
                    		message: '操作成功',
                    		type: 'success'
                  })
                }else {
                  this.$message({
                    message: '操作失败',
                    type: 'error'
                  })
                }
                this.saveLoading=false; //关闭加载框
                this.saveFormVisible=false; //关闭对话框
              }).catch(res=>{
                this.$message.error("系统繁忙,请稍后重试!!![400,404]")
              })
						});
					}
				});
			},
      //8. 选项改变时 会触发此逻辑
			selsChange: function (sels) {
				this.sels = sels;
			},
			//9.批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)//.toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					//let para = { ids: ids };
					this.$http.patch("/dictionarydetail",ids).then(res =>{
            console.log(ids)
            this.listLoading = false;
            if(res.data.success){
              this.currentPage=1;
              this.getDictionarydetails();//删除后 重新查询数据
            }
          })
				}).catch(res => {
          this.$message.error("系统繁忙,请稍后重试!!![400,404]")
				});
			},
      //10.获取部门经理 - 所有的员工
      getTypes(){
        this.$http.get("/dictionarytype").then(res => {
          this.types = res.data;
        })
      },
		},
		mounted() {
			this.getDictionarydetails();
		}
	}

</script>

<style scoped>

</style>